package cn.xsly.user.controller;

import cn.xsly.basic.exception.BusinessException;
import cn.xsly.basic.util.AjaxResult;
import cn.xsly.user.dto.LoginDto;
import cn.xsly.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService iLogininfoService;


    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            return iLogininfoService.accountLogin(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后重试");
        }
    }

    @PostMapping("wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> map){
        try {
            String code = map.get("code");
            return iLogininfoService.wechatLogin(code);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后重试");
        }

    }

    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map) {
        try {
            return iLogininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后重试!!!");
        }
    }



}

